#include "hal_slave_floor.h"
#include "hk32c030xx_gpio.h"

static void slave_floor_ctrl_init() {
	
	GPIO_InitTypeDef floor_key;
	RCC_AHBPeriphClockCmd(FLOOR_SLAVE_PC_RCU | FLOOR_SLAVE_LOCK_RCU | FLOOR_SLAVE_DOOR_RCU, ENABLE);
	
	floor_key.GPIO_Pin = SLAVE_PC_PIN;
	floor_key.GPIO_Mode = GPIO_Mode_IN;
	floor_key.GPIO_OType = GPIO_OType_PP;
	floor_key.GPIO_Speed = GPIO_Speed_10MHz;
	floor_key.GPIO_PuPd = GPIO_PuPd_UP;
	floor_key.GPIO_Schmit = GPIO_Schmit_Disable;
	GPIO_Init(SLAVE_PC_BASE, &floor_key);
	
	floor_key.GPIO_Pin = SLAVE_LOCK_PIN;
	GPIO_Init(SLAVE_LOCK_BASE, &floor_key);

	floor_key.GPIO_Mode = GPIO_Mode_OUT;
    floor_key.GPIO_OType = GPIO_OType_PP;
	
	floor_key.GPIO_Pin = SLAVE_DOOR_PIN;
	GPIO_Init(SLAVE_DOOR_BASE, &floor_key);
	
	GPIO_ResetBits(SLAVE_DOOR_BASE,SLAVE_DOOR_PIN);
}

static uint8_t slave_floor_lock() {
	if(GPIO_ReadInputDataBit(SLAVE_LOCK_BASE,SLAVE_LOCK_PIN))
		return 0;
	else 
		return 1;
}

static void slave_floor_door_ctrl(uint8_t flag) {
	GPIO_WriteBit(SLAVE_DOOR_BASE,SLAVE_DOOR_PIN,flag);
}

static uint8_t slave_floor_get_pc() {
	if(GPIO_ReadInputDataBit(SLAVE_PC_BASE,SLAVE_PC_PIN))
		return 0;
	else 
		return 1;
}

const slave_floor_ctrl_t g_slave_ctrl_ = {
	slave_floor_ctrl_init,
	slave_floor_door_ctrl,
	slave_floor_lock,
	slave_floor_get_pc,
};